package cn.bgs.wjx.WjxService.lmpl;

import cn.bgs.DealershipManagement.pojo.VehicleArea;
import cn.bgs.DealershipManagement.pojo.VehicleCar;
import cn.bgs.DealershipManagement.pojo.VehicleMiddle;
import cn.bgs.DealershipManagement.pojo.VehicleNumber;
import cn.bgs.wjx.WjxMapper.CarMapper;
import cn.bgs.wjx.WjxService.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CarServicelmpl implements CarService {
    @Autowired
    private CarMapper middleMapper;

    @Override
    public List<VehicleMiddle> findAllMyCar(Integer userId) {
        List<VehicleMiddle> vehicleMiddles=middleMapper.findAllMyCar(userId);
        return vehicleMiddles;
    }

    @Override
    public List<VehicleArea> findAllArea() {
        List<VehicleArea> areas=middleMapper.findAllArea();
        return areas;
    }

    @Override
    public List<VehicleNumber> findAllNum() {
        List<VehicleNumber> numbers = middleMapper.findAllNum();
        return numbers;
    }

    @Override
    public List<VehicleCar> findAllCar() {
        List<VehicleCar> cars=middleMapper.findAllCar();
        return cars;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addCarport(VehicleMiddle middle) {
        middleMapper.addCarport(middle);
        middleMapper.updateCarState(middle.getVehicleCarId());
        middleMapper.updateVehicleState(middle.getVehicleNumberId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteCarportUser(Integer vehicleMiddleId,Integer vehicleCarId,Integer vehicleNumberId) {
        middleMapper.deleteCarportUser(vehicleMiddleId);
        middleMapper.deleteCar(vehicleCarId);
        middleMapper.deleteCarNumber(vehicleNumberId);
    }

    @Override
    public void updateCarportUser(VehicleMiddle middle) {
        if(middle.getVehicleCarId() == middle.getVehicleCarOldId() ){
            middleMapper.updateCarportUser(middle);
        }else {
            //将新的车辆 改为 已绑定
            middleMapper.updateCarState(middle.getVehicleCarId());
            //将旧的车辆 改成 未绑定
            middleMapper.updateCarStateOld(middle.getVehicleCarOldId());
            //修改中间表
            middleMapper.updateCarportUser(middle);
        }

        if(middle.getVehicleNumberId() == middle.getVehicleNumberOldId()){
            middleMapper.updateCarportUser(middle);
        }else {
            //将新的车位 改成 审核中
            middleMapper.updateVehicleState(middle.getVehicleNumberId());
            //将旧的车位 改成 未绑定
            middleMapper.updateNumStateOld(middle.getVehicleNumberOldId());
            //修改中间表
            middleMapper.updateCarportUser(middle);
        }
    }

    @Override
    public void updateCancleBinding(VehicleMiddle middle) {
        System.out.println("--------------------------"+middle);
        //将旧的车辆 改成 未绑定
        middleMapper.updateCarStateOld(middle.getVehicleCarId());
        //将旧的车位 改成 未绑定
        middleMapper.updateNumState(middle.getVehicleNumberId());
        //修改中间表
        middleMapper.updateCarportCar(middle);
    }
}
